#include "LevelViewForPreset.h"
#include <QPaintEvent>
#include <QPainter>
#include <QList>


LevelViewForPreset::LevelViewForPreset(QWidget *parent) :
   QWidget(parent),
   m_painter(NULL)
{
}

/**
 * @brief this class must know preset values to color the bars (and write value)
 *        according to passed values.
 * @param values - list of levels in range [0-100] (percent)
 */
void LevelViewForPreset::setLevelValues(const QList<double> &values)
{
   m_levels = values;
}

void LevelViewForPreset::paintEvent(QPaintEvent *)
{
   int widthPerItem = width() / m_levels.length();

   m_painter = new QPainter(this);
   m_painter->setFont(QFont(QString(), -1, QFont::Bold));

   for (int i = 0; i < m_levels.length(); ++i)
   {
      QRect levelRect( i*widthPerItem, 2, widthPerItem * 9 / 10, height() * 9 / 10);
      int levelPercent = (int)(m_levels[i]);  /* clipped to integer value */

      Q_ASSERT( levelPercent >= 0);
      Q_ASSERT( levelPercent <= 100);

      int blackHeight = levelRect.height() * (100 - levelPercent) / 100;
      int yellowHeight = levelRect.height() - blackHeight;

      QRect blackRect = QRect( levelRect.topLeft(), QSize( levelRect.width(), blackHeight) );
      QRect yellowRect = QRect( blackRect.bottomLeft(), QSize( levelRect.width(), yellowHeight) );

      m_painter->fillRect( blackRect, Qt::black);
      m_painter->fillRect( yellowRect, Qt::yellow);

      /* write level twice with two colors so it's visible both in clear and dark zone */
      QString textValue =  QString("%1").arg( levelPercent );
      writeLevelValue( textValue, Qt::cyan, levelRect, blackRect);
      writeLevelValue( textValue, Qt::blue, levelRect, yellowRect);
   }

   delete m_painter;
}

/**
 * @brief write the string passed in \p textValue, clipped to the given \p clipArea
 * @param textValue - printed text
 * @param color - text color
 * @param textArea - Area that the text would occupy if not clipped.
 *    Needed to center the text
 * @param clipArea - clip area; used to write the text in more than one color
 */
void LevelViewForPreset::writeLevelValue(const QString &textValue, const QColor &color,
                                         const QRect &textArea, const QRect &clipArea)
{
   if (textArea.width() >= 20)
   {
      m_painter->save();

      m_pen.setColor( color );
      m_painter->setPen( m_pen );
      m_painter->setClipRect( clipArea);
      m_painter->drawText( textArea, Qt::AlignCenter, textValue );

      m_painter->restore();
   }
}
